package com.weic.design.strategy;

public class Sorter {

    public void sort(Comparable[] arr){
        //确认边界
        if(null == arr || arr.length<2){
            return;
        }
        int n = arr.length;
        for (int i = 0; i < n-1; i++) {
            for (int j = 0; j < n-1-i; j++) {
                Comparable comparable1 = arr[j];
                Comparable comparable2 = arr[j+1];
                if(comparable1.compareTo(comparable2)>0){
                    swap(arr,j,j+1);
                }
            }
        }
    }

    /**
     * 数据交换逻辑
     * @param arr
     * @param idx1
     * @param idx2
     */
    private void swap(Comparable[] arr,int idx1,int idx2){
        Comparable temp = arr[idx1];
        arr[idx1] = arr[idx2];
        arr[idx2] = temp;
    }
}
